package com.demo.steammanagement.controller.admin;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.demo.steammanagement.common.QueryBuild;
import com.demo.steammanagement.common.ResultMsg;
import com.demo.steammanagement.config.response.BizException;
import com.demo.steammanagement.model.entity.GameAccounts;
import com.demo.steammanagement.model.entity.Games;
import com.demo.steammanagement.model.vo.GamesVO;
import com.demo.steammanagement.model.vo.Result;
import com.demo.steammanagement.service.IGameAccountsService;
import com.demo.steammanagement.service.IGamesService;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("/game")
public class GameController {

    @Autowired
    private IGamesService iGamesService;
    @Autowired
    private IGameAccountsService gameAccountsService;

    @GetMapping("/page")
    public IPage<Games> page(GamesVO vo) {

        if (ObjectUtils.isEmpty(vo)) {
            throw new BizException(ResultMsg.OBJECT_IS_EMPTY.getMsg());
        }

        Page<Games> page = new Page<>(vo.getPageNo(), vo.getPageSize());

        return iGamesService.page(page, QueryBuild.gamesQueryWrapper(vo));

     }

     @PostMapping("/saveOrUpdate")
     public Result saveOrUpdate(GamesVO vo) throws IOException {
         // 获取账号总数
         LambdaQueryWrapper<GameAccounts> lambdaQueryWrapper = new LambdaQueryWrapper<>();
         lambdaQueryWrapper.eq(GameAccounts::getGameId, vo.getId());
         long count = gameAccountsService.count(lambdaQueryWrapper);
         vo.setTotalAccounts((int) count);
         LambdaQueryWrapper<GameAccounts>  queryWrapper = new LambdaQueryWrapper<>();
         queryWrapper.eq(GameAccounts::getGameId, vo.getId()).eq(GameAccounts::getStatus, 0);
         // 可租用数
         long count1 = gameAccountsService.count(queryWrapper);
         vo.setAvailableAccounts((int) count1);
         iGamesService.customSaveOrUpdate(vo);
        return Result.success();
     }

    @GetMapping("/get")
    public Result get(GamesVO vo) {

        if (ObjectUtils.isEmpty(vo) || ObjectUtils.isEmpty(vo.getId())) {
            throw new BizException(ResultMsg.OBJECT_IS_EMPTY.getMsg());
        }
        // 获取账号总数
        LambdaQueryWrapper<GameAccounts> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(GameAccounts::getGameId, vo.getId());
        long count = gameAccountsService.count(lambdaQueryWrapper);

        LambdaQueryWrapper<GameAccounts>  queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(GameAccounts::getGameId, vo.getId()).eq(GameAccounts::getStatus, 0);
        // 可租用数
        long count1 = gameAccountsService.count(queryWrapper);
        Games games = iGamesService.getById(vo.getId());
        games.setTotalAccounts((int) count);
        games.setAvailableAccounts((int) count1);
        return Result.success(games);

    }

    @GetMapping("/delete")
    public Result delete(GamesVO vo) {

        if (ObjectUtils.isEmpty(vo) || ObjectUtils.isEmpty(vo.getId())) {
            throw new BizException(ResultMsg.OBJECT_IS_EMPTY.getMsg());
        }
        return Result.success(iGamesService.removeById(vo.getId()));

    }

}
